package com.bxd.baodan.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.bxd.baodan.R;

/**
 * dp/像素转换
 * 
 * @author oldfeel
 * 
 *         Create on: 2014年4月25日
 */
public class DensityUtil {
	public static final int SCREEN_SMALL = 1;
	public static final int SCREEN_NORMAL = 2;
	public static final int SCREEN_LARGE = 3;
	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	 public static int sp2px(Context context, float spValue) {  
         final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
         return (int) (spValue * fontScale + 0.5f);  
     }  
	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	/**
	 * 判断使用大中小分辨率图片
	 * */
	public static int getImgType(Activity context){
		DisplayMetrics metric = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metric);
        Drawable drawable = context.getResources().getDrawable(R.drawable.defaultpic);
//        drawable
        int width = drawable.getIntrinsicWidth();  // 屏幕宽度（像素）
       // ldpi是120dpi，mdpi是160dpi，hdpi是240dpi，xhdpi是320dpi 
        if(width >= 72){
        	return SCREEN_LARGE;
        }else if(width >= 48){
        	return SCREEN_NORMAL;
        }else if(width >= 36){
        	return SCREEN_SMALL;
        }
        	return SCREEN_SMALL;
	}
	public static int getWindowWidth(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		return  wm.getDefaultDisplay().getWidth();

	}
	public static int getWindowHeight(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		return  wm.getDefaultDisplay().getHeight();
	}
}